package Uno;

/**
 *
 * @author Sagor
 */
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import java.util.Random;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Deck extends JFrame    {
    private GridBagLayout deckLayout= new GridBagLayout();                  //creates a GridBagLayout for deck;
    private GridBagConstraints deckConstraints= new GridBagConstraints();   //creates constraints for deckLayout;
    private JButton extraCards;
    private JLabel enterName;
    private JTextField nameField= new JTextField(10);
    private ImageIcon tempCardImage[]= new ImageIcon [112];
    private ImageIcon permCardImage[]= new ImageIcon[112];
    private ImageIcon tempFlippedCardImage[]= new ImageIcon[112];
    private ImageIcon permFlippedCardImage[]= new ImageIcon[112];
    private Random rand= new Random();
    private CallListener call = new CallListener();
    private int p1Deck[]= new int [112];
    private int p2Deck[]= new int [112];
    private int p3Deck[]= new int [112];
    private int p1Cards= 7, p2Cards=7, p3Cards=7;
    private String playerName="";
    private cardButtons[] cardButton= new cardButtons [112];

    public Deck() {                                     //constructor;
        super("Uno!");                                  //call to JFrame constructor that sets title as "Uno!";
        setLayout(deckLayout);                          //sets layout for deck;
        askName();                                      //asks user to enter name;
        initCardImages();                               //initializes permanent and temporary ImageIcons for each card;
    }

    private void initDeck()   {
        for(int i=0, j=0; i<p1Cards && j<14; i++, j+=2)  {
            int k= rand.nextInt(112);
            if(tempFlippedCardImage[k]==null)   {
                i--;
                j-=2;
            }
            else    {
                cardButton[k]=new cardButtons(k, tempFlippedCardImage[k]);
                addComponent(cardButton[k], 2, j+2, 1, 2);
                p1Deck[i]=k;
                tempCardImage[k]=null;
                tempFlippedCardImage[k]=null;
            }
        }

        for(int i=0, j=0; i<p2Cards && j<14; i++, j+=2)  {
            int k= rand.nextInt(112);
            if(tempFlippedCardImage[k]==null)   {
                i--;
                j-=2;
            }
            else    {
                cardButton[k]= new cardButtons(k, tempFlippedCardImage[k]);
                addComponent(cardButton[k], 21, j+2, 1, 2);
                p2Deck[i]=k;
                tempCardImage[k]=null;
                tempFlippedCardImage[k]=null;
            }
        }

        for(int i=0, j=0; i<p3Cards && j<14; i++, j+=2)  {
            int k= rand.nextInt(112);
            if(tempCardImage[k]==null)   {
                i--;
                j-=2;
            }
            else    {
                cardButton[k]= new cardButtons(k, tempCardImage[k]);
                addComponent(cardButton[k], j+6, 16, 2, 1);
                p3Deck[i]=k;
                tempCardImage[k]=null;
                tempFlippedCardImage[k]=null;
            }
        }
    }

    private void resetDeck(int a)   {
        for(int p=0; p<112; p++)    {
            if(p1Deck[p]==a)    {
                p1Deck[p]=113;
                p1Cards--;
            }
            if(p2Deck[p]==a)    {
                p2Deck[p]=113;
                p2Cards--;
            }
            if(p3Deck[p]==a)    {
                p3Deck[p]=113;
                p3Cards--;
            }
        }

        tempCardImage[a]=permCardImage[a];
        tempFlippedCardImage[a]=permFlippedCardImage[a];

        reInitDeck();
    }

    private void reInitDeck()    {
        for(int i=0; i<112; i++)    {
            if(cardButton[i]!=null)  {
                cardButton[i].setVisible(false);
            }
        }

        for(int p=0, j=0; p<112 && j<14; p++, j+=2)    {
            if(p1Deck[p]!= 113) {
                cardButton[p1Deck[p]]=new cardButtons(p1Deck[p], tempFlippedCardImage[p1Deck[p]]);
                addComponent(cardButton[p1Deck[p]], 2, j+2, 1, 2);
                tempCardImage[p1Deck[p]]=null;
                tempFlippedCardImage[p1Deck[p]]=null;
            }

            else    {
                j-=2;
            }
        }

        for(int p=0, j=0; p<112 && j<14; p++, j+=2)    {
            if(p2Deck[p]!= 113) {
                cardButton[p2Deck[p]]=new cardButtons(p2Deck[p], tempFlippedCardImage[p2Deck[p]]);
                addComponent(cardButton[p2Deck[p]], 21, j+2, 1, 2);
                tempCardImage[p2Deck[p]]=null;
                tempFlippedCardImage[p2Deck[p]]=null;
            }

            else    {
                j-=2;
            }
        }

        for(int p=0, j=0; p<112 && j<14; p++, j+=2)    {
            if(p3Deck[p]!= 113) {
                cardButton[p3Deck[p]]=new cardButtons(p3Deck[p], tempFlippedCardImage[p3Deck[p]]);
                addComponent(cardButton[p3Deck[p]], j+6, 16, 2, 1);
                tempCardImage[p3Deck[p]]=null;
                tempFlippedCardImage[p3Deck[p]]=null;
            }

            else    {
                j-=2;
            }
        }
    }

    private void addComponent(Component cards, int column, int row, int height, int width)   {
        deckConstraints.gridx= column;
        deckConstraints.gridy= row;
        deckConstraints.gridheight= height;
        deckConstraints.gridwidth= width;
        deckLayout.setConstraints(cards, deckConstraints);
        add(cards);
    }

    private void askName()    {
        enterName= new JLabel("Enter your name and press Enter: ");
        addComponent(enterName, 12, 8, 1, 1);
        addComponent(nameField, 14, 8, 1, 1);
        nameField.addActionListener(new AccessListener());
    }

    private void initOthers()   {
        JLabel p1= new JLabel("Computer 1");
        JLabel p2= new JLabel("Computer 2");
        JLabel p3= new JLabel(String.format(playerName));
        addComponent(p1, 6, 7, 2, 1);
        addComponent(p2, 18, 7, 2, 1);
        addComponent(p3, 12, 15, 1, 2);

        JButton uno= new JButton("Uno!");
        uno.setEnabled(false);
        addComponent(uno, 12, 2, 1, 1);

        extraCards= new JButton(new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/extra.jpg"));
        addComponent(extraCards, 12, 8, 1, 1);
        extraCards.addActionListener(new AccessListener());
    }

    private void addCallListener()  {
        for(int i=0; i<cardButton.length; i++)   {
            if(cardButton[i]!= null)    {
                cardButton[i].addActionListener(call);
            }
        }
    }

    private void initCardImages()    {
        permCardImage[0]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b10.jpg");
        permCardImage[1]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b11.jpg");
        permCardImage[2]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b12.jpg");
        permCardImage[3]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b13.jpg");
        permCardImage[4]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b14.jpg");
        permCardImage[5]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b15.jpg");
        permCardImage[6]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b16.jpg");
        permCardImage[7]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b17.jpg");
        permCardImage[8]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b18.jpg");
        permCardImage[9]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b19.jpg");
        permCardImage[10]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b20.jpg");
        permCardImage[11]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b21.jpg");
        permCardImage[12]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b22.jpg");
        permCardImage[13]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b23.jpg");
        permCardImage[14]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b24.jpg");
        permCardImage[15]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b25.jpg");
        permCardImage[16]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b26.jpg");
        permCardImage[17]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b27.jpg");
        permCardImage[18]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b28.jpg");
        permCardImage[19]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/b29.jpg");
        permCardImage[20]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r10.jpg");
        permCardImage[21]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r11.jpg");
        permCardImage[22]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r12.jpg");
        permCardImage[23]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r13.jpg");
        permCardImage[24]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r14.jpg");
        permCardImage[25]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r15.jpg");
        permCardImage[26]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r16.jpg");
        permCardImage[27]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r17.jpg");
        permCardImage[28]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r18.jpg");
        permCardImage[29]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r19.jpg");
        permCardImage[30]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r20.jpg");
        permCardImage[31]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r21.jpg");
        permCardImage[32]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r22.jpg");
        permCardImage[33]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r23.jpg");
        permCardImage[34]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r24.jpg");
        permCardImage[35]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r25.jpg");
        permCardImage[36]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r26.jpg");
        permCardImage[37]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r27.jpg");
        permCardImage[38]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r28.jpg");
        permCardImage[39]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/r29.jpg");

        for(int i=0; i<112; i++)   {
            tempCardImage[i]= permCardImage[i];
            permFlippedCardImage[i]= new ImageIcon("C:/Documents and Settings/Sagor.HOME-4994E035B6/My Documents/My Pictures/democards/extra.jpg");
            tempFlippedCardImage[i]= permFlippedCardImage[i];
        }
    }

    class CallListener implements ActionListener   {
        public void actionPerformed(ActionEvent event)  {
            cardButtons cB= (cardButtons) event.getSource();
            resetDeck(cB.index);
            //reInitDeck();
            //JOptionPane.showMessageDialog(null, String.format("%d", cB.index));
        }
    }

    class AccessListener implements ActionListener  {
        public void actionPerformed(ActionEvent event)  {
            if(event.getSource()==nameField)    {
                playerName= String.format("%s", event.getActionCommand());
                nameField.setVisible(false);
                enterName.setVisible(false);
                initDeck();
                initOthers();
                addCallListener();
            }

            if(event.getSource()==extraCards)   {
                javax.swing.JOptionPane.showMessageDialog(null, String.format("No Extra Cards Yet!"));
            }
        }
    }

    class cardButtons extends JButton    {
        int index;
        cardButtons(int index, ImageIcon image)   {
            super(String.valueOf(index));
            this.index= index;
            setIcon(image);
        }
    }
}
